import android.app.Activity
import android.content.Context
import android.content.Intent
import android.util.DisplayMetrics
import com.ug_project.system.nowTimeString
import me.ezitku.base.ScreenRecord.service.ScreenRecordService
import me.ezitku.base.extensions.screenRecoderPath

/**
 * ئېكراننى سىنغا ئېلىش مۇلازىمىتى
 */
/**
 * بۇ فونكىسسيەنى چوقۇم onActivityResult نىڭ ئىچىدە ئىجرا قىلىمىز
 */
fun Activity.startScreenRecorder(resultCode: Int, requestCode: Int, data: Intent, REQUEST_CODE: Int, action: ScreenRecoderConfig.() -> Unit) {
    if (requestCode == REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            var config = ScreenRecoderConfig()
            action.invoke(config)

            val metrics = DisplayMetrics()
            windowManager.defaultDisplay.getMetrics(metrics)
            var mScreenWidth = metrics.widthPixels
            var mScreenHeight = metrics.heightPixels
            var mScreenDensity = metrics.densityDpi


            val service = Intent(this, ScreenRecordService::class.java)
            service.putExtra("code", resultCode)
            service.putExtra("data", data)
            service.putExtra("audio", config.isAudio)
            service.putExtra("width", mScreenWidth)
            service.putExtra("height", mScreenHeight)
            service.putExtra("density", mScreenDensity)
            service.putExtra("quality", config.isVideoSd)
            service.putExtra("filePath", config.filePath)
            startService(service)
        }
    }
}

/**
 * ئېكراننى سىنغا ئېلىشنى توختىتىش
 */
fun Context.stopScreenRecoder() {
    stopService(Intent(this, ScreenRecordService::class.java))
}


class ScreenRecoderConfig {
    var isAudio = false//ئاۋازنى ئۈنگە ئالامدۇ ياقمۇ
    /**
     * ئېنىقلىقى نورمال بولامدۇ ياقمۇ
     *  سۈكۈتتە نورمال بولىدۇ
     *
     */
    var isVideoSd = true
    /**
     * سىننى ساقلايدىغان ئادرىس
     */
    var filePath = screenRecoderPath + nowTimeString("yyyy-MM-dd-HH-mm-ss")
}